package com.szqhyxj.youxiangju.biz.plan.preview;

import android.content.Intent;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.BasePresenterImpl;
import com.szqhyxj.youxiangju.biz.plan.PlanDetail;
import com.szqhyxj.youxiangju.biz.plan.PlanModel;
import com.szqhyxj.youxiangju.sys.BaseResult;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class PlanPreviewPresenter extends BasePresenterImpl implements PlanPreviewContract.Presenter {

    private PlanPreviewContract.View view;

    PlanPreviewPresenter(PlanPreviewContract.View view) {
        this.view = view;
    }

    private PlanModel.PlanCreateEntity planCreateEntity;

    private PlanDetail planDetail;

    private String planId;

    @Override
    public void start(Intent intent) {
        this.planId = intent.getStringExtra(PlanPreviewContract.PLAN_ID);
        this.planCreateEntity = intent.getParcelableExtra(PlanPreviewContract.PARAM);
        startRequest(
                PlanModel.preview(planId)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseConsumer<PlanDetail>() {
                            @Override
                            protected void error(String code, String msg){
                                view.dismissLoading();
                            }

                            @Override
                            protected void successAccept(PlanDetail planDetail) {
                                PlanPreviewPresenter.this.planDetail = planDetail;
                                view.showPlan(planDetail);
                            }
                        })
        );
    }

    @Override
    public void reCreatePlan() {
        view.showLoading();
        startRequest(
                PlanModel.create(this.planCreateEntity)
                        .subscribeOn(Schedulers.io())
                        .flatMap((Function<BaseResult<PlanModel.PlanCreateResult>, ObservableSource<BaseResult<PlanDetail>>>) createResult -> {
                            if (createResult.businessLogicFail()) {
                                return Observable.error(new Throwable(createResult.getMessage()));
                            } else {
                                String planId = createResult.getData().getPlanId();
                                this.planId = planId;
                                return PlanModel.preview(planId);
                            }
                        })
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseConsumer<PlanDetail>() {
                            @Override
                            protected void error(String code, String msg){
                                view.dismissLoading();
                            }

                            @Override
                            protected void successAccept(PlanDetail planDetail) {
                                PlanPreviewPresenter.this.planDetail = planDetail;
                                view.showPlan(planDetail);
                                view.dismissLoading();
                                view.showToast("重新规划成功");
                            }
                        })
        );
    }

    @Override
    public void applyPlan() {
        view.showLoading();
        startRequest(
                PlanModel.apply(this.planId)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseConsumer<Object>() {
                            @Override
                            protected void successAccept(Object o) {
                                view.showApplySuccess(PlanPreviewPresenter.this.planDetail);
                                view.dismissLoading();
                            }

                            @Override
                            public void error(String code, String msg){
                                view.dismissLoading();
                            }
                        })
        );
    }

}